<!--
 * @Descripttion: 
 * @version: 
 * @Author: lehaixia
 * @Date: 2023-04-18 10:09:08
 * @LastEditors: lehaixia
 * @LastEditTime: 2023-04-28 13:46:55
-->
<template>
  <List ref="qmpList" v-model:finished="finished" :reset-fun="resetData" :load="load" :refresh="load">
    <Item v-for="(item, i) in list" :key="i" :item-data="item" @edit="toEdit(item)" @enable="handleEnable(item)" />
  </List>
</template>

<script lang="ts">
  import { ref, reactive, toRefs, defineComponent, watch, getCurrentInstance, nextTick } from 'vue'
  import { useRouter } from 'vue-router'
  import List from '@/components/List/index.vue'
  import Item from './Item.vue'
  import { getManagetList, enableOrDisable, detail } from '@/apis/attractInvestment/leasingManager'

  interface datasModle {
    list: any[]
    finished: boolean
  }

  export default defineComponent({
    name: 'ManagerList',
    components: { List, Item },
    props: {
      searchValue: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: ''
      }
    },
    setup(props) {
      const { proxy }: any = getCurrentInstance()
      const qmpList = ref()
      const $router = useRouter()
      const datas = reactive<datasModle>({
        list: [],
        finished: false
      })
      const load = async (page: number, rows: number) => {
        const filtersRaw: object[] = []
        if (props.searchValue) {
          filtersRaw.push({
            id: '',
            property: '[managerName,managerPhone]',
            operator: 'OR LIKE',
            value: props.searchValue
          })
        }
        const paramsData = {
          page,
          rows,
          sort: 'lastUpdateTime',
          order: 'desc',
          filtersRaw,
          paramsRaw: {},
          sortersRaw: [],
          pageFlag: true
        }
        return getManagetList(paramsData).then((res) => {
          const resData = res.data
          const list = resData.records
          if (page === 1) {
            datas.list = list
          } else {
            datas.list = datas.list.concat(list)
          }
          if (resData.total <= datas.list.length) {
            datas.finished = true
          }
        })
      }
      const resetData = () => {
        datas.list = []
      }
      const resetPageData = () => {
        qmpList.value.resetPageData()
      }

      const toEdit = (row: any) => {
        $router.push({
          path: '/leasingManager/edit',
          query: {
            id: row.id
          }
        })
      }

      const refreshItem = (id: string) => {
        const flag = datas.list.some((v) => v.id === id)
        if (!flag) return

        detail(id).then((res) => {
          const d = res.data
          if (d && d.id) {
            datas.list = datas.list.map((item: any) => {
              if (item.id === id) {
                item = { ...res.data }
              }
              return item
            })
          }
        })
      }

      const handleEnable = (row: any) => {
        const url = row.enable === 1 ? 'disable' : 'enable'
        enableOrDisable([row.id], url).then((res: any) => {
          if (res.success === 1) {
            proxy.$toast(`${row.enable === 1 ? '停用' : '启用'}成功`)

            nextTick(() => {
              refreshItem(row.id)
            })
          }
        })
      }

      watch(
        () => props.searchValue,
        () => {
          resetPageData()
        }
      )

      return {
        ...toRefs(datas),
        load,
        resetData,
        qmpList,
        resetPageData,
        toEdit,
        handleEnable,
        refreshItem
      }
    }
  })
</script>
<style lang="less" scoped></style>
